package com.zhiyun.message;

import java.util.List;

import org.json.JSONObject;

import com.gzit.utils.AndroidUtils;
import com.zhiyun.net.MessageDelegate;
import com.zhiyun.net.Pair;
import com.zhiyun.net.Request;
import com.zhiyun.net.Response;

public class VersionMessageDelegate extends MessageDelegate {

	public static class Req extends Request {

		@Override
		public List<Pair> getParams() {
			return Pair.EMPTY_PAIRS;
		}

		@Override
		public void genMetaData() {
			this.setActionPath("a/version");
		}

	}

	public static class Resp extends Response {

		private String version;
		private String url;

		public boolean needDownload() {
			if (!AndroidUtils.System.getVersionName().equals(version)) {
				return true;
			}
			return false;
		}

		@Override
		protected void decodeBody(JSONObject bodyJSON) throws Exception {

			JSONObject data = bodyJSON.getJSONObject("data");

			version = getString(data, "version");
			url = getString(data, "url");

			// test...
//			url = "http://3g.kingdee.com:6899/3ginfo/download?file=EC232.apk";

		}

		public String getUrl() {
			return url;
		}

		public void setUrl(String url) {
			this.url = url;
		}

		public String getVersion() {
			return version;
		}

		public void setVersion(String version) {
			this.version = version;
		}

	}

	public Req req = new Req();
	public Resp resp = new Resp();

	@Override
	public Request request() {
		return req;
	}

	@Override
	public Response response() {
		return resp;
	}

}
